package com.zxbit.flash.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.zxbit.flash.PopApplication;
import com.zxbit.flash.cache.Acache;
import com.zxbit.flash.cache.AcacheKeys;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.network.RetrofitHelper;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.bean.IpAddress;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AppLink {
    private static AppLink appLink;
    private String userAgent = null;

    public static AppLink getInstance() {
        if (appLink == null) {
            appLink = new AppLink();
        }
        return appLink;
    }

    public String getTimeStamp() {
        String time = System.currentTimeMillis() / 1000 / 6000 + "";
        return time;
    }


    public String getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;// 屏幕宽度（像素）
        float density = dm.density;//屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;//屏幕密度dpi（120 / 160 / 240）
        //屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);//屏幕宽度(dp)
        return String.valueOf(screenWidth);
    }

    public void init() {
        getFirgureInfo(PopApplication.getContext());
        Acache.get(PopApplication.getContext()).put(AcacheKeys.IS_INSTALL, "1");


    }

    public void getFirgureInfo(final Context context) {
        final StringBuffer sb = new StringBuffer();
        String model = Build.MODEL;
        if (model.contains("Xiaomi ")) {
            model = model.replace("Xiaomi ", "");
        }
        sb.append(model);
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder().url("http://pv.sohu.com/cityjson").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                DebugLog.d(e.toString());
            }

            @SuppressLint("CheckResult")
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                result = result.replace("var returnCitySN = ", "");
                result = result.replace(";", "");
                Gson gson = new Gson();
                IpAddress address = gson.fromJson(result, IpAddress.class);
                sb.append(address.getCip());
                String info=sb.toString().replaceAll(" ","");
                String sendMessage = Sha1Utils.encode(info);
                String device_id = UniqueUtils.getUniquePsuedoID();

                Map<String, Object> map = new HashMap<>();
                map.put("deviceFingerprint", sendMessage);
                map.put("uniqueIdentifier", device_id);

                RetrofitHelper.getRetorfitHelper().getApiService().appInstall(ParamsFactory.getInstallAppParams(map)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<BlankResponse>() {
                    @Override
                    public void accept(BlankResponse response) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
            }
        });


    }


}
